package com.y.architecture.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.y.architecture.R;
import com.y.architecture.base_net.ApiCallback;
import com.y.architecture.base_refresh_load.BaseRefreshLoadActivity;
import com.y.architecture.dialog.TipsPopupWindow;
import com.y.architecture.recycle_util.BaseQuickAdapter;
import com.y.architecture.recycle_util.BaseViewHolder;
import com.y.architecture.util.AFConstant;
import com.y.architecture.util.AFSizeUtil;
import com.y.architecture.weather.Weather;
import com.y.architecture.weather.WeatherService;
import com.y.zxing.android.CaptureActivity;

import butterknife.ButterKnife;

public class MainActivity extends BaseRefreshLoadActivity<Weather.HeWeather6Bean> implements BaseQuickAdapter.OnItemLongClickListener {


//    @BindView(R.id.button)
//    Button btn;

    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    public String getTitleText() {
        return "首页";
    }

    @Override
    public void onToolbarClicked(View view) {
        super.onToolbarClicked(view);
//        Toast.makeText(getAFContext(),"---",Toast.LENGTH_LONG).show();
    }

    @Override
    public void initViews(Bundle savedInstanceState) {

//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                WeatherService.getInstance().getData("厦门", new ApiCallback<Weather>() {
//                    @Override
//                    public void onError(Throwable e) {
//                        Toast.makeText(MainActivity.this,"--"+e.getMessage(),Toast.LENGTH_LONG).show();
//                        AFLog.w(e.getMessage());
//                    }
//
//                    @Override
//                    public void onNext(Weather weather) {
//                        Toast.makeText(MainActivity.this,"--"+ weather.getHeWeather6().get(0).getStatus(),Toast.LENGTH_LONG).show();
//                    }
//                });
////                RxLocation.get().locate(MainActivity.this)
////                        .subscribe(new LocationSubscriber() {
////                            @Override
////                            public void onLocatedSuccess(@NonNull AMapLocation bdLocation) {
////                                //定位成功
////                                //定位成功回调信息，设置相关消息
////                                AFLog.w( bdLocation.getCity());
////                                AFLog.w( bdLocation.getAddress());
////                                AFLog.w( bdLocation.getLocationDetail());
////                                Toast.makeText(MainActivity.this,"定位："+bdLocation.getLocationDetail(),Toast.LENGTH_LONG).show();
////                            }
////
////                            @Override
////                            public void onLocatedFail(AMapLocation bdLocation) {
////                                //定位失败
////                                AFLog.w("定位失败");
////                                Toast.makeText(MainActivity.this,"定位错误码："+bdLocation.getErrorCode(),Toast.LENGTH_LONG).show();
////                            }
////                        });
//            }
//        });

        autoRefresh();

        mAdapter.setOnItemLongClickListener(this);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1000) {
            if (data == null) {
                return;
            }
            switch (resultCode) {
                case CaptureActivity.RESULT_SUCCESS:
                    String resultSuccess = data.getStringExtra(CaptureActivity.INTENT_KEY_RESULT_SUCCESS);
                    showToast(resultSuccess);

                    break;
                case CaptureActivity.RESULT_FAIL:
                    String resultError = data.getStringExtra(CaptureActivity.INTENT_KEY_RESULT_ERROR);
                    showToast(resultError);
                    break;
                case CaptureActivity.RESULT_CANCLE:
                    showToast("取消扫码");
                    break;
            }
        }
    }

    @Override
    public BaseQuickAdapter<Weather.HeWeather6Bean, BaseViewHolder> getAdapter() {
        return new BaseQuickAdapter<Weather.HeWeather6Bean, BaseViewHolder>(R.layout.item_text, mItems) {
            @Override
            protected void convert(BaseViewHolder helper, Weather.HeWeather6Bean item) {
                helper.setText(R.id.tv_item, item.getNow().getTmp() + helper.getLayoutPosition());
            }
        };
    }


    @Override
    public void loadData(final int page) {
//        mSwipeRefreshLayout.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//        loadComplete();
        WeatherService.getInstance().getData("厦门", new ApiCallback<Weather>() {
            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(Weather weather) {
                if (page == AFConstant.REFRESH_FIRST_PAGE) {
                    mItems.clear();
                }
//                        for (int i = 0; i < 15; i++) {
                mItems.addAll(weather.getHeWeather6());
//                        }
                refreshLoadComplete(isSuccess(weather.getHeWeather6()));
            }
        });
        refreshComplete();

//                refreshLoadComplete(isSuccess(mItems));
//            }
//        }, 500);
    }
    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        TipsPopupWindow tipsPopupWindow = new TipsPopupWindow(this, findViewById(R.id.v_temp));
        tipsPopupWindow.showAtBottomLeft(AFSizeUtil.dp2px(16), -AFSizeUtil.dp2px(16));
        return true;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        showToast("one:"+position);
//        startActivity(QrCodeActivity.class,null);
        Intent intent = new Intent(this, CaptureActivity.class);
        //是否显示相册按钮
        intent.putExtra(CaptureActivity.INTENT_KEY_PHOTO_FLAG, true);
        //识别声音
        intent.putExtra(CaptureActivity.INTENT_KEY_BEEP_FLAG, true);
        //识别震动
        intent.putExtra(CaptureActivity.INTENT_KEY_VIBRATE_FLAG, true);
        //扫码框的颜色
        intent.putExtra(CaptureActivity.INTENT_KEY_SCSNCOLOR, "#FFFF00");
        //扫码框上面的提示文案
        intent.putExtra(CaptureActivity.INTENT_KEY_HINTTEXT, "请将二维码放入框中....");
        startActivityForResult(intent, 1000);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WeatherService.getInstance().onDestory();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }
}
